from django.db import models


class BookManager(models.Manager):
    """自定义模型管理类"""
    def all(self):
        return super().filter(is_delete=False)

# Create your models here.
# 先设计表 及表中的字段, 约束
# book.image.url  #  booktest.timg.jpeg
class BookInfo(models.Model):
    """图书模型"""
    # 字段名(表中字段) = 字段类型(约束/选项)
    btitle = models.CharField(max_length=20, verbose_name='名称')
    bpub_date = models.DateField(verbose_name='发布日期')
    bread = models.IntegerField(default=0, verbose_name='阅读量')
    bcomment = models.IntegerField(default=0, verbose_name='评论量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')
    # 如果表已存在,并且表中已有数据,模型新追加的字段必须给默认值或允许为空
    image = models.ImageField(upload_to='booktest', verbose_name='图片', null=True)


    # book_manager = BookManager()

    class Meta:  # 元类作用: 配置当前模型 type
        db_table = 'tb_books'
        verbose_name = '图书'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.btitle

    def bread_fo(self):
        return '%d次' % self.bread

    bread_fo.short_description = '阅读量'  # 修改方法名在在列表界面展示时的中文名
    bread_fo.admin_order_field = 'bread'

# 定义英雄模型类HeroInfo
class HeroInfo(models.Model):
    GENDER_CHOICES = (
        (0, 'female'),
        (1, 'male')
    )
    hname = models.CharField(max_length=20, verbose_name='名称')
    hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name='性别')
    hcomment = models.CharField(max_length=200, null=True, verbose_name='描述信息')
    hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name='图书')  # 外键
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_heros'
        verbose_name = '英雄'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hname




